<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>

<body>
	<canvas style="border:1px solid #d3d3d3;">
		<script>
    
    //去掉重复的元素。
    var arr = [1,15,24,1,2,15,1,11,2,14,2];//输出[1, 15, 24, 2, 11, 14]
    var data = [];
    (
      function(arr) {
      var object = {};
      for(var i=0;i<arr.length;i++) {
        if(!object[arr[i]]) {
          object[arr[i]] = true;
          data.push(arr[i]);
        }
      }
      return data;
    }
    )(arr);
    // console.log(data);

    //统计一个字符串出现最多的字母
    var str ='sdadassdsdsawdecdfaadsadasdsadsdasa';
    var maxChar = '';
    (
      function(str) {
        var object = {};
        if(str.length <= 1) {
          return str;
        }

        for(var i=0;i<str.length;i++) {
          if(object[str.charAt(i)]) {
            object[str.charAt(i)] += 1;
          } else {
            object[str.charAt(i)] = 1;
          }
        }

        var count = 1;
        for(var k in object) {
          if(object[k] >= count) {
            count = object[k];
            maxChar = k;
          }
        }
        return maxChar;
      }
    )(str);
    // console.log(maxChar);

    // 不借助中间变量实现两个变量的值互换
    var a = 125;
    var b = 1110;//输出a=11，b=5；
    (
      function(a,b) {
        a = a -b;
        b = b + a;
        a = b -a;
        return (this.a = a,this.b = b);
      }
    )(a,b);
    //console.log('a = ' + a);
    //console.log('b = ' + b);

    // 排序算法
    var pxArr = [0,5,9,1,4,8,3,7,2,6];
      (
        function(pxArr) {
          var long = pxArr.length;
          var tem;
         if(long <= 1) { return pxArr;}
          for (let i=0;i<long-1;i++) {
            for(var j=i+1;j<long;j++) {
              if(pxArr[i] > pxArr[j]) {
                tem = pxArr[i];
                pxArr[i] = pxArr[j];
                pxArr[j] = tem;
              }
            }
          }
          return pxArr;
        }
     )(pxArr);
    //console.log(pxArr);

    //使用canvas 绘制一个有限度的斐波那契数列的曲线
    var canvas = document.querySelector('canvas');
    canvas.width = 600;
    canvas.height = 480;
    var coor = {
      x: 300,
      y: 240,
    };
    var ctx = canvas.getContext('2d');
    function draw(r, n ,prevR) {
      if(n>2) {
        switch(n%4) {
          case 0 :
            coor.y = coor.y - 5 * prevR;
            coor.y = coor.y + 5 * r;
            break;
          case 1 :
            coor.x = coor.x + 5 * prevR;
            coor.x = coor.x - 5 * r;
            break;
          case 2 :
            coor.y = coor.y + 5 * prevR;
            coor.y = coor.y - 5 * r;
            break;
          case 3 :
            coor.x = coor.x - 5 * prevR;
            coor.x = coor.x + 5 * r;
            break;
        }
      }
      ctx.beginPath();
      ctx.arc(coor.x,coor.y,5*r,Math.PI*0.5*(n),Math.PI*0.5*(n-1),true);
      if(n>1) {
        switch(n%4) {
          case 0 :
            ctx.moveTo(coor.x - 5*r,coor.y);
            break;
          case 1 :
            ctx.moveTo(coor.x,coor.y + 5*r);
            break;
          case 2 :
          ctx.moveTo(coor.x + 5*r,coor.y);
            break;
          case 3 :
            ctx.moveTo(coor.x,coor.y-5*r);
            break;
        }
      }
      ctx.lineWidth = 1;
      ctx.strokeStyle = '#000';
      ctx.stroke();
    }
    function getFibonacci(n) {
      var fibarr = [];
      var i = 0;
      while(i<n) {
        if(i<=1) {
          fibarr.push(i);
        }else{
          fibarr.push(fibarr[i-1] + fibarr[i-2])
        }
        i++;
      }
      return fibarr;
    }
    var data = getFibonacci(10);
    for(var i = 0,l=data.length;i<l;i++) {
      if(data[i]!=0) {
        draw(data[i],i,data[i-1]);
        
      }
    }
    // 找出正数组的最大差值
    var amountArr = [1,0,12,2,9,1212,6,4,551]
    var amount;
    (
      function(amountArr) {
        var minPrice = amountArr[0];
        amount = 0;//存放差值
        for(var i=0;i<amountArr.length;i++) {
          var currentPrice = amountArr[i];
          minPrice = Math.min(minPrice, currentPrice);
          var potentialProfit = currentPrice - minPrice;
          amount = Math.max(amount, potentialProfit);
        }
        return amount;
      }
    )(amountArr);
    // console.log(amount);

    //随机生成指定长度的字符串
    var randomStr = 'QWERTYUIOPASDFGHJKLZXCVBNM';
    var result;
    (
      function(n) {
        result = '';
        var k;
        var long = randomStr.length;
        for (var i=0;i<n;i++) {
          k=Math.floor(Math.random()*long);
          result += randomStr.charAt(k);
        }
        return result;
      }
    )(10);
    //console.log(result);
  </script>
</body>

</html>